package com.park.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.model.AuthUser;
import com.park.model.Authorityadmin;
import com.park.service.AuthorityService;
import com.park.service.AuthorityadminService;
import com.park.service.Utility;

@Controller
@RequestMapping("authorityadmin")
public class AuthorityadminController {
	@Autowired
	AuthorityadminService authorityadminService;
	
	@Autowired
	AuthorityService authorityService;
	
	@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String insert(@RequestBody Authorityadmin authorityadmin){
		Map<String, Object> result=new HashMap<>();
		int num=authorityadminService.insert(authorityadmin);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="getByToken",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String getByToken(@RequestBody Map<String, Object> args){
		Map<String, Object> result=new HashMap<>();
		String token=(String) args.get("token");
		List<Authorityadmin> authorityadmins=authorityadminService.getByToken(token);
		if (!authorityadmins.isEmpty()) {
			result.put("status", 1001);
			result.put("body", authorityadmins.get(0));
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	@RequestMapping(value="update",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public String update(@RequestBody Authorityadmin authorityadmin){
		Map<String, Object> result=new HashMap<>();
		int num=authorityadminService.updateByPrimaryKeySelective(authorityadmin);
		if (num==1) {
			result.put("status", 1001);
		}
		else {
			result.put("status", 1002);
		}
		return Utility.gson.toJson(result);
	}
	
	@RequestMapping(value = "/loginIn", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String loginIn(@RequestBody Map<String, String> args){
		System.out.println("----进入新的登陆方法----"+args);
		String userName=args.get("username");
		String password=args.get("password");
		AuthUser users=authorityService.getByUserNameAndPassword(userName, password);
		Map<String, Object> ret = new HashMap<String, Object>();
		System.out.println(users);
		if (users!=null) {
			ret.put("status", 1001);
			ret.put("token", userName);
		}
		else {
			ret.put("status", 1002);
			ret.put("message", "用户名或密码错误");
		}
		String result = Utility.gson.toJson(ret);
		System.out.println("----result----"+result);
		return result;
	}
	
	@RequestMapping(value = "/getByUserName", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getByUserName(@RequestBody Map<String, String> args){
		String userName=args.get("username");
		AuthUser users=authorityService.getUserByUsername(userName);
		List<String> roles = new ArrayList<String>();
		roles.add("admin");
		Map<String, Object> ret = new HashMap<String, Object>();
		if (users!=null) {
			ret.put("status", 1001);
			ret.put("roles", roles);
			ret.put("name", userName);
			ret.put("body", users);
		}
		else {
			ret.put("status", 1002);
			ret.put("message", "获取用户信息失败");
		}
		return Utility.gson.toJson(ret);
	}
	
	
}
